# -*- coding: utf-8 -*-

# -------------------------------
# Author    : linlingwei
# Email     : 951035650@qq.com
# File Name : install.py
# -------------------------------

import os
import sys
import getpass

srcPath = os.path.join(os.path.dirname(__file__))
srcPath = os.path.normpath(srcPath)
try:
    import maya.mel
    import maya.cmds
    isMaya = True
except ImportError:
    isMaya = False

def __modules():
    """

    """
    user_name = getpass.getuser()
    # C:\Users\Admin\Documents\maya\modules
    modules_path = os.path.join(r"C:\Users",user_name,r"Documents\maya\modules")
    if not os.path.exists(modules_path):
        os.makedirs(modules_path)
    modules_file = "correctShape.mod"
    modules_file_path = os.path.join(modules_path,modules_file)
    if not os.path.exists(modules_file_path):
        command = (r"""
+ MAYAVERSION: {ver} correctShape any {scrpts_path}
plug-ins: .\plug-ins\{ver}
PYTHONPATH += {scrpts_path}
""")
        with open(modules_file_path,"w") as mf:
            mf.write("".join([command.format(ver=ver,scrpts_path=os.path.join(srcPath,"scripts"))
                        for ver in ["2018","2019","2020","2022"]]))
    return

def _onMayaDropped(p=True):
    """Dragging and dropping this file into the scene executes the file."""
    command = (
r'''
# -----------------------------------
# correctShape
# Email : 951035650@qq.com
# -----------------------------------

import os
import sys
import platform
path = r'{path}'
if path not in sys.path:
    sys.path.append(path)
import correctShape
pyversion = platform.python_version_tuple()[0]
if pyversion == "2":
    from imp import reload
    reload(correctShape)
elif pyversion == "3":
    from importlib import reload
    reload(correctShape)
correctShape.create()
'''.format(path=os.path.join(srcPath,"scripts")))

    shelf = maya.mel.eval('$gShelfTopLevel=$gShelfTopLevel')
    parent = maya.cmds.tabLayout(shelf, query=True, selectTab=True)
    maya.cmds.shelfButton("CreateCorrectShape",
        command=command,
        label="CCS",
        image = "pythonFamily.png",
        image1 ="pythonFamily.png",
        annotation='correctShape',
        sourceType='Python',
        parent=parent
    )

def setup():
    maya_version = maya.cmds.about(v=True)
    os.environ["MAYA_PLUG_IN_PATH"] += ";"+os.path.join(srcPath,"plug-ins",maya_version)
    return
if isMaya:
    __modules()
    _onMayaDropped()
    setup()
    
if __name__ == '__main__':
    pass